<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Admin\Controller\InitController;
/**
 * 后台会员模型管理
 */
class MemberController extends InitController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('Member');
		$this->module_db = D('Module');
		$this->model_db = D('Model');
		$this->pagecurr = max(1, I('page', 0, 'intval'));
		$this->pagesize = 10;
	}

	/**
	 * 会员管理
	 * @author xuewl <master@xuewl.com>
	 */
	public function manage() {
		$sqlMap = array();		
		$grouplist = getcache('member_group', 'member');
		$modellists = getcache('model', 'commons');
		foreach ($grouplist as $k => $v) $grouplist[$k] = $v['name'];		
		$modellist = array();
		foreach ($modellists as $k => $v) {
			if($v['module'] != 'member' || $v['disabled'] == 1) continue;
			$modellist[$k] = $v['name'];
		}		
		if (submitcheck('search')) {
			$info = I('param.');
			$info['start_time'] = (!empty($info['start_time'])) ? strtotime($info['start_time']) : 0;
			$info['end_time'] = (!empty($info['end_time'])) ? strtotime($info['end_time']) : 0;
			/* 注册时间 */
			if ($info['start_time'] > 0) {
				$sqlMap['regdate'] = array("EGT", $info['start_time']);
			}
			if ($info['end_time'] > 0) {
				$sqlMap['regdate'] = array("ELT", $info['end_time']);
			}
			/* 当前状态 */
			$info['status'] = (int) $info['status'];
			if ($info['status'] > 0) {
				$sqlMap['islock'] = $info['status'];
			}
			/* 会员模型 */
			$info['modelid'] = (int) $info['modelid'];
			if ($info['modelid'] > 0) {
				$sqlMap['modelid'] = $info['modelid'];
			}
			/* 会员组 */
			$info['groupid'] = (int) $info['groupid'];
			if ($info['groupid'] > 0) {
				$sqlMap['groupid'] = $info['groupid'];
			}
			/* 关键字搜索类型 */
			$info['type'] = (int) $info['type'];
			if (trim($info['keyword'])) {
				switch ($info['type']) {
					case '1': //用户名
						$sqlMap['username'] = array("LIKE", "%".$info['keyword']."%");
						break;
					case '2': // 用户ID
						$sqlMap['userid'] = array("LIKE", "%".intval($info['keyword'])."%");
						break;
					case '3': // 注册邮箱
						$sqlMap['email'] = array("LIKE", "%".$info['keyword']."%");
						break;
					case '4': // 注册IP
						$sqlMap['regip'] = array("LIKE", "%".$info['keyword']."%");
						break;						
					default:
						$sqlMap['nickname'] = array("LIKE", "%".$info['keyword']."%");
						break;
				}
			}
		}
		$membercount = $this->db->where($sqlMap)->count();
		$memberlist = $this->db->where($sqlMap)->page($this->pagecurr, $this->pagesize)->select();
		$pages = page($membercount, $this->pagesize);
		/* 附加菜单 */
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\''.U('add').'\', title:\''.L('member_add').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('member_add'));
		$form =  new \Common\Library\form();
		include $this->admin_tpl('member_list');
	}

	/**
	 * 添加会员
	 * @author xuewl <master@xuewl.com>
	 */
	public function add() {
		$models = getcache('model', 'commons');
		$modelid = I('modelid', '1', 'intval');
		$setting = getcache('setting', 'member');
		if (submitcheck('dosubmit')) {
			$info = I('post.info');
			$info['username'] = $info['email'];
			$info['modelid'] = $modelid;
			$MemberLogic = D('Member', 'Logic');
			$result = $MemberLogic->register($info);
			if (!$result) {
				$this->error($MemberLogic->getError());
			}	
			$this->success('注册成功','javascript:close_dialog()');			
		} else {
			$form =  new \Common\Library\form();
			$grouplist = $this->model_db->getField('modelid,name');
			include $this->admin_tpl('member_add');
		}
	}
	/**
	 * 验证登陆名是否被注册
	 */
	public function verify_username(){
		$username = $_REQUEST['username'];
			$tmps = $this->db->where(array('username'=>$username))->find();
			if(!$tmps){
				echo '1';
			}else{
				echo '2';
			}
	}

	/**
	 * 验证昵称是否被注册
	 */
	public function verify_nickname(){
		$nickname = $_REQUEST['nickname'];
			$tmps = $this->db->where(array('nickname'=>$nickname))->find();
			if(!$tmps){
				echo '1';
			}else{
				echo '2';
			}
	}
	/**
	 *  验证电话号码是否被注册
	 */
	public function verify_mobile(){
		$mobile = $_REQUEST['mobile'];
			$tmps = $this->db->where(array('mobile'=>$mobile))->find();
				if(!$tmps){
					echo '1';
				}else{
					echo '2';
				}
	}

	/**
	 * 验证邮箱是否被注册
	 */
	public function verify_email(){
		$email = $_REQUEST['email'];
			$tmps = $this->db->where(array('email'=>$email))->find();
			if(!$tmps){
				echo '1';
			}else{
				echo '2';
			}
	}
    /**
     * 给予奖励
     */
	public  function reward(){
	    $userid = I('userid');			
		$info = $this->db->where(array('userid'=>$userid))->find();
		include $this->admin_tpl('member_reward');
	}
	
	public  function reward_add(){
			$info = I('post.info');
			$rs = array_filter($info);
			$coin=$rs['coin']+$rs['u_coin'];
			$bean=$rs['bean']+$rs['u_bean'];
			$reason=$rs['reason'];
			if (!empty($coin)){
			score_change($rs['userid'],$reason,$rs['u_coin'],'u_coin',1);
			}
			if (!empty($bean)){
			score_change($rs['userid'],$reason,$rs['u_bean'],'u_bean',1);
			}
			$result=$this->db->where(array('userid'=>$rs['userid']))->save(array('u_coin'=>$coin,'u_bean'=>$bean
	));
			if ($result){
			     $this->success('操作成功', 'javascript:close_dialog();', 2);
			}else{
				$this->error("操作失败");
			}
		
	
	}
	/**
	 * 编辑会员
	 * @author xuewl <masater@xuewl.com>
	 */
	public function edit() {
		if (submitcheck('dosubmit')) {
			$info = I('post.info');
			$rs = array_filter($info);
			$userinfo = $this->db->where(array('userid'=>$info['userid']))->find();
			if($rs['password']){
				$rs['password'] = md5(md5($rs['password'].$userinfo['encrypt']));
			}
			$result = $this->db->where(array('userid'=>$rs['userid']))->save($rs);
			if(!$result){
				$this->error("操作失败");
			}
			$this->success("修改成功");

		} else {			
			$userid = I('userid');			
			$info = $this->db->where(array('userid'=>$userid))->find();
			$grouplist = $this->model_db->getField('modelid,name');
			$form =  new \Common\Library\form();
			include $this->admin_tpl('member_edit');
		}				
	}

	public function delete() {
		$models = getcache('model','commons');
		if (IS_POST && I('fromhash') == session('FROMHASH')) {
			$userid = (array) $_POST['userid'];
			if (!empty($userid)) {
				foreach ($userid as $uid) {
					$modelid = $this->db->getFieldByUserid($uid, 'modelid');
					if ($models[$modelid]['module'] == 'member') {
						$tablename = $models[$modelid]['tablename'];
						if ($tablename) {
							M($tablename)->where(array('userid' => $uid))->delete();
						}
					}
					$this->db->where(array('userid' => $uid))->delete();
				}
			}
			$this->success('操作成功');
		} else {
			$this->error('请勿非法访问');
		}
	}

	/**
	 * 会员锁定
	 * @author xuewl <master@xuewl.com>
	 */
	public function lock() {
		if (IS_POST && I('fromhash') == session('FROMHASH')) {
			$userid = (array) $_POST['userid'];
			if (!empty($userid)) {
				foreach ($userid as $uid) {
					$this->db->where(array('userid' => $uid))->setField('islock','1');
				}
			}
			$this->success('已将指定会员锁定');
		} else {
			$this->error('请勿非法访问');
		}				
	}

	/**
	 * 会员解锁
	 * @author xuewl <master@xuewl.com>
	 */
	public function unlock() {
		if (IS_POST && I('fromhash') == session('FROMHASH')) {
			$userid = (array) $_POST['userid'];
			if (!empty($userid)) {
				foreach ($userid as $uid) {
					$this->db->where(array('userid' => $uid))->setField('islock','0');
				}
			}
			$this->success('已将指定会员解锁');
		} else {
			$this->error('请勿非法访问');
		}			
	}

	/**
	 * 会员审核
	 * @author xuewl <master@xuewl.com>
	 */
	public function verify() {
		if (submitcheck('dosubmit')) {
			$params = I('request.');
			$uids = (array) $params['userid'];
			$type_var = array('pass' => '1', 'reject' => '-1', 'ignore' => '-2');
			$sqlMap = array();
			$sqlMap['userid'] = array("IN", $uids);
			if ($params['type'] != 'delete') {
				$status_val = $type_var[$params['type']];
				$this->db->where($sqlMap)->setField('status', $status_val);
				/* 发送邮件通知 */
				if ($params['sendemail'] && $params['message']) {
					foreach ($uids as $key => $uid) {
						$email = $this->db->getFieldByUserid($uid, 'email');
						helpers('mail');
						sendemail($email, '会员审核通知', $params['message']);
					}
				}
			} else {
				$this->db->where($sqlMap)->delete();
			}
			$this->success('操作成功');
		} else {
			$member_model = getcache('model', 'commons');
			$verify_status = array('-2' => '忽略', '-1' => '拒绝', '0' => '待审', '1' => '通过');
			$sqlMap = array();
			$sqlMap['status'] = array("NEQ", 1);
			$membercount = $this->db->where($sqlMap)->count();
			$memberlist = $this->db->where($sqlMap)->page($this->pagecurr, $this->pagesize)->select();
			$pages = page($membercount, $this->pagesize);
			include $this->admin_tpl('member_verify');
		}	
	}

	/**
	 * 查询会员资料
	 * @author xuewl <master@xuewl.com>
	 */
	public function memberinfo($userid = 0) {
		$userid = (int) $userid;
		include $this->admin_tpl('member_moreinfo');
	}

	public function setting() {
		$models = getcache('model', 'commons');
		foreach ($models as $k => $v) {
			if($v['module'] != 'member' || $v['disabled'] == 1) unset($models[$k]);
		}
		$_grouplist = getcache('member_group', 'member');
		$grouplist = array();
		foreach ($_grouplist as $k => $v) {
			if($k < 2) continue;
			$grouplist[$k] = $v['name'];
		}

		$member_setting = $this->module_db->getFieldByModule('Member', 'setting');
		$member_setting = dstripslashes(unserialize($member_setting));
		if (submitcheck('dosubmit')) {
			$info = $_POST['info'];
		    $cash_config = array();
		    foreach ($info['cash_config']['money'] as $k => $v) {
		    	if(empty($v)) continue;
		    	$cash_config[$v] = $info['cash_config']['number'][$k+1];
		    }
		    $info['cash_config'] = $cash_config;
			$result = $this->module_db->where(array('module' => 'Member'))->setField('setting', serialize($info));
			if (!$result) {
				$this->error('模块配置失败');
			}
			setcache('setting', $info, strtolower(MODULE_NAME));
			$this->success('操作成功');
		} else {
			$form = new \Common\Library\form();
			include $this->admin_tpl('member_setting');			
		}
	}
	
	public function agent(){
		$id=I('param.');
		$sqlMap=array();
		$sqlMap['agent_id']=array('EQ',$id['userid']);
		
		$agentcount = $this->db->where($sqlMap)->count();
		$agentlist = $this->db->where($sqlMap)->page($this->pagecurr, $this->pagesize)->select();
	    $pages = page($agentcount, $this->pagesize);
	    	/* 附加菜单 */
	   //$form =  new \Common\Library\form();
	   include $this->admin_tpl('agent_list');		
	}
}